﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Admin_PhanCongViec : System.Web.UI.Page
{
    DataClassesDataContext data = new DataClassesDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadThongTin();
            LoadDanhSach();
            LoadThongtinGiangVien();
        }
    }

    private void LoadThongtinGiangVien()
    {
        if (Request.QueryString["MSGV"]!=null)
        {
            string msgv = Request.QueryString["MSGV"];
            var gvs = from p in data.tbl_GIANGVIENs
                        where p.MsGiangVien == msgv
                        select p;
            foreach (tbl_GIANGVIEN gv in gvs)
            {
                txtHoTenGV.Text = gv.TenGiangVien;
            }
            txtMSGV.Text = msgv;
            txtMSGV.Enabled = false;

        }
        else
        { 
        }
    }

    private void LoadDanhSach()
    {
        GridView_PhanCong.DataSource = data.tbl_PHANCONGVIECs;
        GridView_PhanCong.DataBind();
    }

    private void LoadThongTin()
    {
        ddlMonHoc.DataSource = data.tbl_MONHOCs;
        ddlMonHoc.DataTextField = "TenMonHoc";
        ddlMonHoc.DataValueField = "MsMonHoc";
        ddlMonHoc.DataBind();

        var query = from p in data.tbl_LOPTCs
                    where p.MsMonHoc == ddlMonHoc.SelectedValue.ToString()
                    select p;
        dllLopTC.DataSource = query;
        dllLopTC.DataTextField = "MsLopTC";
        dllLopTC.DataValueField = "MsLopTC";
        dllLopTC.DataBind();
    }

    
    protected void ddlMonHoc_SelectedIndexChanged(object sender, EventArgs e)
    {
        var query = from p in data.tbl_LOPTCs
                    where p.MsMonHoc == ddlMonHoc.SelectedValue.ToString()
                    select p;
        dllLopTC.DataSource = query;
        dllLopTC.DataTextField = "MsLopTC";
        dllLopTC.DataValueField = "MsLopTC";
        dllLopTC.DataBind();
    }
    protected void GridView_GiangVien_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView_PhanCong.PageIndex = e.NewPageIndex;
        LoadDanhSach();
    }
    protected void GridView_GiangVien_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "MyDelete")
        {
            string[] commandArgs = e.CommandArgument.ToString().Split(new char[] { ',' });
            string msgv = commandArgs[0];
            string msltc = commandArgs[1];

            var XoaHP = from p in data.tbl_PHANCONGVIECs
                        where p.MsGiangVien == msgv && p.MsLopTC == msltc
                        select p;
            foreach (tbl_PHANCONGVIEC xoaHP in XoaHP)
            {
                data.tbl_PHANCONGVIECs.DeleteOnSubmit(xoaHP);
            }
            data.SubmitChanges();
            LoadDanhSach();
        }
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "this.setAttribute('bgColor', this.style.backgroundColor); this.style.backgroundColor = '#FF6600';");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor = this.getAttribute('bgColor');");
            //tbl_PHANCONGVIEC sv = e.Row.DataItem as tbl_PHANCONGVIEC;
            //(e.Row.FindControl("ImageButton2") as ImageButton).CommandArgument = sv.MsGiangVien.ToString();
        }
    }
    protected void BtTimGV_Click(object sender, EventArgs e)
    {
        string msgv = txtMSGV.Text;
        var gvs = from p in data.tbl_GIANGVIENs
                  where p.MsGiangVien == msgv
                  select p;
        if (gvs.Count() > 0)
        {
            foreach (tbl_GIANGVIEN gv in gvs)
            {
                txtHoTenGV.Text = gv.TenGiangVien;
            }
        }
        else
        {
            txtHoTenGV.Text = "";
            ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Giảng viên không tồn tại!');", true);
        }
    }
    protected void btnSavePerson_Click(object sender, EventArgs e)
    {
        if (txtMSGV.Text == "")
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Chưa có thông tin giảng viên!');", true);
            return;
        }
        tbl_PHANCONGVIEC phancong = new tbl_PHANCONGVIEC();
        
        phancong.MsGiangVien = txtMSGV.Text;
        phancong.MsLopTC = dllLopTC.SelectedValue;
        
        data.tbl_PHANCONGVIECs.InsertOnSubmit(phancong);
        data.SubmitChanges();
        LoadDanhSach();
    }
}
